<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Paging {
	function __construct() {
	}
	
	public static function setPagination($params=array()){
		$ci = &get_instance();
		$ci->load->library('pagination');
		
		$config['base_url']   = $params['base_url'];
		$config['uri_segment'] = $params['uri_segment'];
		$config['total_rows'] = $params['total_rows'];
		$config['per_page']   = $params['per_page'];
		
		//optional: currently used for index only, where multiple pagination works because we reset the cur_page value ok
		if(isset($params['cur_page'])) $config['cur_page'] = $params['cur_page'];
		$ci->pagination->initialize($config); 
		$pagination_link = $ci->pagination->create_links();
		return $pagination_link;
	}
	
	public static function embedSuffixInPaging($embedString,$pagination){ //to manipulate pagination link to embed the suffix needed
		$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
		$unique = array();
		if( preg_match_all("/$regexp/siU", $pagination, $matches) ){
			foreach ( $matches[2] as $link ){
				if ( !isset($unique[$link]) ){
					$pagination = str_replace($link . '"', $link . $embedString . '"', $pagination);
					$unique[$link] = '';
				}
			}
		}
		unset($unique);
		return $pagination;
	}
	
}